﻿using SqlSugar;

public class SqlSugarContext
{
    public SqlSugarClient Db { get; }

    public SqlSugarContext(IConfiguration configuration)
    {
        Db = new SqlSugarClient(new ConnectionConfig
        {
            ConnectionString = configuration.GetConnectionString("Db"),
            DbType = DbType.SqlServer,
            IsAutoCloseConnection = true,
            InitKeyType = InitKeyType.Attribute,
            MoreSettings = new ConnMoreSettings  // 注意类名拼写
            {
                // 仅限 SqlSugarCore >=5.1.4 的版本
                //SqlServerCodeFirstNvarchar = true  // 属性名严格匹配
            }
        });

        // 开启 SQL 日志
        Db.Aop.OnLogExecuting = (sql, pars) =>
        {
            Console.WriteLine($"SQL: {sql}");
        };
    }
}